/*
 * bsp_sha.c
 *
 *  Created on: 2023年7月5日
 *      Author: fjx
 */
#include "bsp_sha.h"
#include <stdio.h>
#include <string.h>
#include "app_log.h"


DS_SHA_STATUS DS_hash_compute(SHA_Type_t type,
                                const uint8_t *input, size_t input_length,
                                uint8_t *hash, size_t hash_size,
                                size_t *hash_length )
{//sha1,sha2_256,sha2_384,sha2_512
  DS_SHA_STATUS ret = DS_SHA_STATUS_SUCCESS;
  psa_algorithm_t alg = PSA_ALG_SHA_256;

  if(type == HASH_SHA1){
      alg = PSA_ALG_SHA_1;
  }
  else if(type == HASH_SHA2_512){
      alg = PSA_ALG_SHA_512;
  }
  else if(type == HASH_SHA2_384){
      alg = PSA_ALG_SHA_384;
  }
  else if(type == HASH_SHA2_224){
        alg = PSA_ALG_SHA_224;
  }


  if( psa_hash_compute(alg,
                        input,
                        input_length,
                        hash,
                        hash_size,
                        hash_length) != DS_SHA_STATUS_SUCCESS){
      ret = DS_SHA_STATUS_FAIL;
  }
  return ret;
}

DS_SHA_STATUS DS_hash_compare(SHA_Type_t type,
                                const uint8_t *input, size_t input_length,
                                uint8_t *hash, size_t hash_length)
{
  DS_SHA_STATUS ret = DS_SHA_STATUS_SUCCESS;
  psa_algorithm_t alg = PSA_ALG_SHA_256;

  if(type == HASH_SHA1){
      alg = PSA_ALG_SHA_1;
  }
  else if(type == HASH_SHA2_512){
      alg = PSA_ALG_SHA_512;
  }
  else if(type == HASH_SHA2_384){
      alg = PSA_ALG_SHA_384;
  }
  else if(type == HASH_SHA2_224){
        alg = PSA_ALG_SHA_224;
  }

  if( psa_hash_compare(alg,
                        input,input_length,
                        hash,hash_length) != DS_SHA_STATUS_SUCCESS){
      ret = DS_SHA_STATUS_FAIL;
  }

  return ret;
}

void DS_hash_stream_operation_init(psa_hash_operation_t * operation, SHA_Type_t type)
{
  DS_SHA_STATUS ret = DS_SHA_STATUS_SUCCESS;
  psa_algorithm_t alg = PSA_ALG_SHA_256;
  psa_hash_operation_t op;

  if(type == HASH_SHA1){
      alg = PSA_ALG_SHA_1;
  }
  else if(type == HASH_SHA2_512){
      alg = PSA_ALG_SHA_512;
  }
  else if(type == HASH_SHA2_384){
      alg = PSA_ALG_SHA_384;
  }
  else if(type == HASH_SHA2_224){
        alg = PSA_ALG_SHA_224;
  }

  ret = psa_crypto_init();
  DS_app_log_info("psa crypto init,ret=%d\n",ret);
  op = psa_hash_operation_init();
  ret = psa_hash_setup(&op, alg);
  memcpy(operation,&op,sizeof(psa_hash_operation_t));
  DS_app_log_info("psa hash setup,ret = %d\n",ret);

}
DS_SHA_STATUS DS_hash_stream_update( psa_hash_operation_t *operation,
                                    const uint8_t *input,
                                    size_t input_length )
{

  return  psa_hash_update(operation,input,input_length)==DS_SHA_STATUS_SUCCESS? DS_SHA_STATUS_SUCCESS:DS_SHA_STATUS_FAIL;
}

DS_SHA_STATUS DS_hash_stream_clone( const psa_hash_operation_t *source_operation,
                                   psa_hash_operation_t *target_operation )
{
  return psa_hash_clone( source_operation,target_operation)==DS_SHA_STATUS_SUCCESS? DS_SHA_STATUS_SUCCESS:DS_SHA_STATUS_FAIL;;

}
DS_SHA_STATUS DS_hash_stream_finish( psa_hash_operation_t *operation,
                              uint8_t *hash,
                              size_t hash_size,
                              size_t *hash_length )
{

  return psa_hash_finish( operation,hash,hash_size,hash_length)==DS_SHA_STATUS_SUCCESS? DS_SHA_STATUS_SUCCESS:DS_SHA_STATUS_FAIL;;
}

DS_SHA_STATUS DS_hash_stream_verify( psa_hash_operation_t *operation,
                              const uint8_t *hash,
                              size_t hash_length )
{
  return psa_hash_verify(operation,hash,hash_length)==DS_SHA_STATUS_SUCCESS? DS_SHA_STATUS_SUCCESS:DS_SHA_STATUS_FAIL;;
}



